package com.qikux.dao.impl;

import com.qikux.annotation.Component;
import com.qikux.dao.UserDao;
import com.qikux.entity.User;
import com.qikux.entity.UserInfo;
import com.qikux.entity.UserScore;
import com.qikux.utils.DBUtils;
import com.qikux.vo.Page;

import java.util.List;
import java.util.Map;

@Component
public class UserDaoImpl implements UserDao {
    @Override
    public User findUserByTel(String tel) {
        String sql = "select * from tb_user where tel = ?";
        return DBUtils.queryForEntity(sql,User.class,tel);
    }

    @Override
    public User findUserByEmail(String email) {
        String sql = "select id ,tel, enable, create_time from tb_user where email = ?";
        return DBUtils.queryForEntity(sql,User.class,email);
    }

    @Override
    public int saveUser(User user) {
        String sql = "insert into tb_user(tel,password,email,create_time) values (#{tel},md5(#{password}),#{email},now())";
        return DBUtils.save(sql,user);
    }

    @Override
    public int saveUserInfo(UserInfo userInfo) {
        String sql = "insert into tb_user_info (photo,birth,nick_name,real_name,sex,user_id) values (#{photo},#{birth},#{nickName},#{realName},#{sex},#{userId})";
        return DBUtils.save(sql,userInfo);
    }

    @Override
    public User findUserById(Long userId) {
        String sql = "select * from tb_user where id = ?";
        return DBUtils.queryForEntity(sql,User.class,userId);
    }

    @Override
    public void updateStatus(long userId, int enable) {
        String sql = "update tb_user set enable = ? where id = ?";
        DBUtils.update(sql,enable,userId);
    }

    @Override
    public Map<String, Object> findScoreConfigByAction(String action) {
        String sql = "select * from tb_score_conf where action = ? and enable = 1";
        return DBUtils.queryForMap(sql,action);
    }

    @Override
    public void saveUserScore(UserScore userScore) {
        String sql = "insert into tb_user_score (user_id,remark,score,res_id,create_time) values (#{userId},#{remark},#{score},#{resId},now())";
        DBUtils.update(sql,userScore);
    }


    @Override
    public UserInfo findUserInfoByUserId(Long id) {
        String sql = "select * from tb_user_info where user_id = ?";
        return DBUtils.queryForEntity(sql,UserInfo.class,id);
    }

    @Override
    public void updatePassWord(Long id, String newPassWord) {
        String sql = "update tb_user set password = md5(?) where id = ?";
        DBUtils.update(sql,newPassWord,id);
    }

    @Override
    public List<Map<String, Object>> findUserStaticsScore(Long userId) {
        String sql = "select remark name,sum(score) value from tb_user_score where user_id = ? group by remark";
        return DBUtils.queryForList(sql,userId);
    }

    @Override
    public List<Map<String, Object>> findUserScoreByUserId(Long userId, Page<Map<String,Object>> pageBean) {
        String sql = """
            select s.remark,s.score,s.create_time,s.res_id,r.resource_name from tb_user_score s left join tb_resource r on
            s.res_id = r.id where s.user_id = ?  order by create_time desc limit ?,?
                """;
        return DBUtils.queryForList(sql,userId,pageBean.getOffset(),pageBean.getRows());
    }

    @Override
    public int findUserScoreCountByUserId(Long id) {
        String sql = "select count(1) from tb_user_score where user_id = ?";
        return DBUtils.execute(sql,rs -> {
            if (rs.next()){
                return rs.getInt(1);
            }
            return 0;
        },id);
    }


    @Override
    public void updateNickNameByUserId(String nickName, Long id) {
        String sql = "update tb_user_info set nick_name = ? where user_id = ?";
        DBUtils.update(sql,nickName,id);
    }

    @Override
    public void updateSexByUserId(String sex, Long id) {
        String sql = "update tb_user_info set sex = ? where user_id = ?";
        DBUtils.update(sql,sex,id);
    }

    @Override
    public void updatePhoto(UserInfo info) {
        String sql = "update tb_user_info set photo = ? where user_id = ?";
        DBUtils.update(sql,info.getPhoto(),info.getUserId());
    }

    @Override
    public void updateEmail(Long id, String newEmail) {
        String sql = "update tb_user set email = ? where id = ?";
        DBUtils.update(sql,newEmail,id);
    }

    @Override
    public void initePassword(Long id) {
        String sql = "update tb_user set password = md5('123456') where id = ?";
        DBUtils.update(sql,id);
    }

    @Override
    public List<Map<String, Object>> findUserAttention(Long userId,Page<Map<String, Object>> pageBean) {
        String sql = """
                select nick_name,user_id uid,photo,status from tb_user_info left join tb_user_focus 
                on tb_user_info.user_id = tb_user_focus.user_id_b
                where user_id in (select user_id_b from tb_user_focus where user_id_a = ?) and status = 1
                limit ?,?
                """;
        return DBUtils.queryForList(sql,userId,pageBean.getOffset(),pageBean.getRows());
    }

    @Override
    public int findUserAttentionCountByUserId(Long userId) {
        String sql = "select count(status) from tb_user_focus where user_id_a = ? and status = 1";
        return DBUtils.execute(sql,rs -> {
            if (rs.next()){
                return rs.getInt(1);
            }
            return 0;
        },userId);
    }

    @Override
    public List<Map<String, Object>> findUserFenNumById(Long id) {
        String sql = "select count(user_id_b) u  from tb_user_focus where user_id_a = 18 and status = 1";
        return DBUtils.queryForList(sql);
    }

    @Override
    public List<Map<String, Object>> findUserFenNumById2(Long userId) {
        String sql = "select count(status) s from tb_user_focus where user_id_b = ? and status = 1";
        return DBUtils.queryForList(sql,userId);
    }

    @Override
    public int uploadStatusByUid(Long uid) {
        String sql ="update tb_user_focus set status = 0 where user_id_b = ? and status = 1";
        return DBUtils.update(sql,uid);
    }

    @Override
    /**
     * @description 根据userid 查询收藏的资源
     * @param Long id
     * @return java.util.List<com.qikux.entity.Resource>
     * @date 2022/9/26 23:02
     * @auther XuHuanDong
     */
    public List<Map<String, Object>> queryCollectResourceByUseId(Long id,Page<Map<String,Object>> pageBean) {
        String sql = """
                select res_id,resource_name,ext,score,collection_time,nick_name,r.user_id 
                from tb_resource_collection c
                join tb_resource r on c.res_id = r.id
                join tb_user_info f on f.user_id = r.user_id
                where c.user_id = ?
                order by collection_time desc limit ? , ?
                """;
        return DBUtils.queryForList(sql,id,pageBean.getOffset(),pageBean.getRows());
    }


    @Override
    public List<Map<String, Object>> queryCollectResByUseId(Long id, Page<Map<String, Object>> pageBean) {
        String sql = "select r.id, resource_name,resource,score,ext,status,type_name,create_time,pid from tb_resource r left join tb_resource_type  t on type_id = t.id where user_id=? order by create_time desc limit ?,? ";

        return DBUtils.queryForList(sql,id,pageBean.getOffset(),pageBean.getRows());
    }

    @Override
    public int queryCollectResourceCountByUseId(Long id) {
        String sql = "select count(1) from tb_resource_collection where user_id = ?";
        return DBUtils.execute(sql,rs -> {
            if (rs.next()){
                return rs.getInt(1);
            }
            return 0;
        },id);
    }

    @Override
    public int queryCollectResCountByUseId(Long id) {
        String sql = "select count(1) from tb_resource where user_id = ?";
        return DBUtils.execute(sql,rs -> {
            if (rs.next()){
                return rs.getInt(1);
            }
            return 0;
        },id);
    }

    @Override
    public Map<String, Object> qureyIdByPid(Long pid) {
        String sql = "select type_name from tb_resource_type where id=?";
        return DBUtils.queryForMap(sql,pid);
    }

    @Override
    public int addAttentionByUid(Long userId, Long uid) {
        String sql = "update tb_user_focus set status=1 where user_id_a = ? and user_id_b = ?";
        return DBUtils.update(sql,userId,uid);
    }

    @Override
    public int deleteAttentionByUid(Long userId, Long uid) {
        String sql ="update tb_user_focus set status=0 where user_id_a = ? and user_id_b = ?";
        return DBUtils.update(sql,userId,uid);
    }

    @Override
    public Map<String, Object> findAttentionStatusById(Long userId, Long rId) {
        String sql = " select status from tb_user_focus where user_id_a = ? and user_id_b = ?";
        return DBUtils.queryForMap(sql,userId,rId);
    }

    @Override
    public Map<String,Object> findAttentionByUserIdAndRid(Long userId, Long rId) {
        String sql = "select * from tb_user_focus where user_id_a = ? and user_id_b = ?";
        return DBUtils.queryForMap(sql,userId,rId);
    }

    @Override
    public int addAttentionByUidAndRid(Long userId, Long rId) {
        String sql = "insert into tb_user_focus(user_id_a,user_id_b,status) values (?,?,0)";
        return DBUtils.update(sql,userId,rId);
    }

    @Override
    /**
     * @description 是否关注资源上传者
     * @param Long id
     * @param Long resUserId
     * @return void
     * @date 2022/10/10 20:25
     * @auther XuHuanDong
     */
    public List<Map<String, Object>> isAttentionResUserId(Long id, Long resUserId) {
        String sql = "select * from tb_user_focus where user_id_a = ? and user_id_b = ? and status != 0";
        return DBUtils.queryForList(sql,id,resUserId);
    }
}
